#!/bin/bash 
echo "___________________Battleship v1.0___________________"
field()
{	 
	for ((i=0;i<100;i++))
		do
			map[i]=" . ";
		done	
}
draw()
{	
	column=0
	row=0
	echo -n "	   "
	for ((row=0;row<11;row++));do
		if [ $row == 0 ];then
			echo ". 0  1  2  3  4  5  6  7  8  9 ";
		else
			for((column=0;column<11;column++));do
				if [ $column == 0 ];then					
					let temp=$row-1;
					echo -n $temp;
				else
					echo -n "${map[10*(row-1)+(column-1)]}"
				fi
			done			
			echo ""
		fi
		echo -n "	   "		
	done	
	echo ""
}
ship_draw()
{
	beg_r=${param:0:1}
	beg_c=${param:1:1}
	end_r=${param:2:1}	
	end_c=${param:3:1}	
	# check the syntax of input variables
	if [[ "$param" =~ ^[0-9]{4}$  ]] && [[ $beg_r == $end_r || $beg_c == $end_c ]];then
	
		# chek the size of ship	
		let lengthW=$end_r-$beg_r+1;
		let lengthH=$end_c-$beg_c+1;
		echo -n "Ship № $par: ";
		if [[ $lengthW > 4 || $lengthH > 4 ]];then echo "Ship is too large!";par=$(($par-1));continue;
			elif [[ $lengthW > $lengthH ]];then w=1;length=$lengthW; echo -n "Length: $length |";
			elif [[ $lengthW < $lengthH ]];then w=0;length=$lengthH; echo -n "Length: $length |";
			else w=1;length=1; echo -n "Length: $length |";
		fi
	
	echo "Coordinates: Begin: "$beg_r,$beg_c"| End: "$end_r,$end_c
		for((i=$beg_r;i <= $end_r;i++));do
			for((j=$beg_c;j <= $end_c;j++));do
				map[10*i+j]="[#]"; #filling array by [#] 
			done				
		done
		else echo "Wrong coordinate!";par=$(($par-1));
	
	fi
}

field;
	par=0;
	for param in $@;do
		par=$(($par+1));
			if [[ $par -gt 10 ]]; then
				echo "You have entered too much coordinates!!! Only 10 ships with CORRECT coordinates will be printed!"
				break
			fi
		ship_draw;
	done
draw;

exit 0;
